/*
Copyright (c) 2020, Huawei Technologies Co., Ltd
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

* Description: 2D图表可视化插件数据组织类
*/
#include "st_space_data.h"

namespace chart_plugin {
using std::vector;
uint32_t StObject::id = 0;

bool StSpaceData::SetCurveData(const vector<custom::StPoint> &pointVector, QPolygonF *qPolygonF) const
{
    if (qPolygonF == nullptr) {
        return false;
    }

    if (!qPolygonF->isEmpty()) {
        qPolygonF->clear();
    }

    for (const auto &point : pointVector) {
        (*qPolygonF).append(QPointF(point.tValue, point.sValue));
    }

    return true;
}

void StSpaceData::SetObjectsCurveData(const vector<custom::StPoint> &pointVector)
{
    objectsMap.clear();
    objectsIdTypeMap.clear();
    objectsTypeIdMap.clear();

    if (pointVector.empty()) {
        return;
    }

    QStringList listId;
    for (const auto &point : pointVector) {
        int32_t type = point.type;
        QString id = QString::fromStdString(point.id);

        listId.push_back(id);
        if (!idIdMap.contains(id)) {
            LimitObjectsNumber(listId);
            LimitIdSize();
            QSharedPointer<StObject> object = QSharedPointer<StObject>(new StObject(id));
            idObjectMap.insert(object->stObjectId, object);
            idIdMap.insert(id, object->stObjectId);
        }

        if (!objectsMap.contains(id)) {
            QPolygonF objectIdNew;
            objectIdNew.append(QPointF(point.tValue, point.sValue));
            objectsMap.insert(id, objectIdNew);
            objectsIdTypeMap.insert(id, type);
            QStringList listId = objectsTypeIdMap.value(type);
            if (!listId.contains(id)) {
                listId.push_back(id);
                objectsTypeIdMap.insert(type, listId);
            }
            continue;
        }
        objectsMap[id].append(QPointF(point.tValue, point.sValue));
    }
}

const QPolygonF &StSpaceData::GetFirstStTrack() const
{
    return firstStTrack;
}

const QPolygonF &StSpaceData::GetOptimizedStTrack() const
{
    return optimizedStTrack;
}

const QPolygonF &StSpaceData::GetStSpeedLimit() const
{
    return stSpeedLimit;
}

const QMap<QString, QPolygonF> &StSpaceData::GetObjectsMap() const
{
    return objectsMap;
}

const QMap<QString, int32_t> &StSpaceData::GetObjectsIdTypeMap() const
{
    return objectsIdTypeMap;
}

const QMap<int32_t, QStringList> &StSpaceData::GetObjectsTypeIdMap() const
{
    return objectsTypeIdMap;
}

void StSpaceData::SetObjectVisible(const QString &id, bool isVisible)
{
    QString stObjectId = idIdMap.value(id);
    idObjectMap.value(stObjectId)->isVisible = isVisible;
}

bool StSpaceData::GetObjectVisible(const QString &id) const
{
    QString stObjectId = idIdMap.value(id);
    return idObjectMap.value(stObjectId)->isVisible;
}

void StSpaceData::Clear()
{
    idObjectMap.clear();
    idIdMap.clear();

    firstStTrack.clear();
    optimizedStTrack.clear();
    stSpeedLimit.clear();
    objectsMap.clear();
    objectsIdTypeMap.clear();
    objectsTypeIdMap.clear();
}

bool StSpaceData::Add(const custom::StSpace::ConstPtr &msg)
{
    if (msg == nullptr) {
        return false;
    }

    const auto &firstStTrackData = msg->firstStTrack;
    const auto &optimizedStTrackData = msg->optimizedStTrack;
    const auto &stSpeedLimitData = msg->stSpeedLimit;
    const auto &stObjectsData = msg->stObjects;

    if ((SetCurveData(firstStTrackData, &firstStTrack) && SetCurveData(optimizedStTrackData, &optimizedStTrack) &&
        SetCurveData(stSpeedLimitData, &stSpeedLimit))) {
        SetObjectsCurveData(stObjectsData);
        return true;
    }
    return false;
}

void StSpaceData::LimitObjectsNumber(const QStringList &listId)
{
    int32_t objectsMaxNumber = 1000;
    if (idObjectMap.count() < objectsMaxNumber) {
        return;
    }

    for (auto iter = idObjectMap.begin(); iter != idObjectMap.end(); iter++) {
        if (!listId.contains(iter.key())) {
            idIdMap.remove(iter.value()->objectId);
            idObjectMap.erase(iter);
            return;
        }
    }
}

void StSpaceData::LimitIdSize()
{
    if (StObject::id < UINT_MAX) {
        return;
    }

    QMap<QString, QSharedPointer<StObject>> map;
    auto objects = idObjectMap.values();
    for (int32_t i = 0; i < objects.count(); i++) {
        auto object = objects.at(i);
        object->setId(i);
        map.insert(QString::number(i), object);
        idIdMap[object->objectId] = QString::number(i);
    }
    idObjectMap.clear();
    idObjectMap.swap(map);
}
} //  namespace chart_plugin
